package com.kj.xuyuan.pop;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.kj.xuyuan.R;
import com.kj.xuyuan.util.DisplayUtil;
import com.kj.xuyuan.util.ScreenUtils;
import com.kj.xuyuan.util.StringUtils;
import com.kj.xuyuan.util.ToastUtil;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class WithDrawPopWindow extends PopupWindow {

    @BindView(R.id.tv_ali)
    TextView tvAli;
    @BindView(R.id.tv_ali_line)
    TextView tvAliLine;
    @BindView(R.id.tv_wx)
    TextView tvWx;
    @BindView(R.id.tv_wx_line)
    TextView tvWxLine;
    @BindView(R.id.ed_name)
    EditText edName;
    @BindView(R.id.ed_account)
    EditText edAccount;
    @BindView(R.id.tv_next)
    TextView tvNext;

    private Activity mContext;
    private View view;
    private int withdrawType = 1;//1支付宝  2微信
    private String account,name;


    public WithDrawPopWindow(Activity context) {
        super(context);
        this.mContext = context;
        // 初始化页面
        init();
    }

    // 初始化布局
    private void init() {
        final LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        view = inflater.inflate(R.layout.layout_pop_withdraw, null);
        ButterKnife.bind(this, view);
        setFocusable(true);

        // 导入布局
        this.setContentView(view);
        this.setClippingEnabled(false);
        setWidth(ScreenUtils.getScreenWidth(mContext)- DisplayUtil.dp2px(mContext,70));
        setHeight(WindowManager.LayoutParams.WRAP_CONTENT);
        setOutsideTouchable(true);
        final ColorDrawable dw = new ColorDrawable(0x0000000);
        setBackgroundDrawable(dw);
        setAlpha(0.7f);
    }

    @Override
    public void dismiss() {
        setAlpha(1);
        super.dismiss();
    }

    private void setAlpha(float alpha) {
        WindowManager.LayoutParams params = mContext.getWindow().getAttributes();
        params.alpha = alpha;
        mContext.getWindow().setAttributes(params);
    }

    @OnClick({R.id.tv_ali, R.id.tv_ali_line, R.id.tv_wx, R.id.tv_wx_line, R.id.tv_next})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_ali:
            case R.id.tv_ali_line:
                withdrawType = 1;
                tvAliLine.setVisibility(View.VISIBLE);
                tvAli.setTextColor(mContext.getResources().getColor(R.color.pink));
                tvWxLine.setVisibility(View.INVISIBLE);
                tvAli.setTextColor(mContext.getResources().getColor(R.color.gray9));
                break;
            case R.id.tv_wx:
            case R.id.tv_wx_line:
                withdrawType = 2;
                tvAliLine.setVisibility(View.INVISIBLE);
                tvAli.setTextColor(mContext.getResources().getColor(R.color.gray9));
                tvWxLine.setVisibility(View.VISIBLE);
                tvAli.setTextColor(mContext.getResources().getColor(R.color.pink));
                break;
            case R.id.tv_next:
                name = edName.getText().toString().trim();
                account = edAccount.getText().toString().trim();
                if(StringUtils.isEmpty(name)){
                    ToastUtil.showToast(mContext,"姓名未填写");
                    return;
                }
                if(StringUtils.isEmpty(account)){
                    ToastUtil.showToast(mContext,"账户未填写");
                    return;
                }
                if(listener != null){
                    listener.withdrawListener(withdrawType,name,account);
                }
                dismiss();
                break;
        }
    }

    private WithdrawListener listener;

    public void setWithdrawListener(WithdrawListener listener){
        this.listener = listener;
    }

    public interface WithdrawListener{
        void withdrawListener(int withdrawType,String name,String account);
    }
}
